//
//  YBChinaListViewController.m
//  GoodBuy
//
//  Created by Cyber on 15/5/13.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "YBChinaListViewController.h"
#import "ChinaDetailViewController.h"
#import "ChinaTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "NetworkHandler.h"
#import "AFNetworking.h"
#import "MJRefresh.h"

@interface YBChinaListViewController () <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
@property (nonatomic, retain) UITableView *tv;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) NSMutableArray  *arrayImages;
@property (nonatomic, retain) NSMutableArray *viewController;
@property (nonatomic, retain) NSTimer *timer;
@end

@implementation YBChinaListViewController
- (void)dealloc
{
    
    [_juhua release];
    [_ArrChina release];
    [_timer release];
    [_arrayImages release];
    [_viewController release];
    [_pageControl release];
    [_scrollView release];
    [_dataArr release];
    [_tv release];
    [super dealloc];
}

//- (void)viewDidAppear:(BOOL)animated
//{
//    self.navigationController.navigationBar.translucent = NO;
//    self.automaticallyAdjustsScrollViewInsets = NO;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"购国内";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
    
    // 调用 ScrollView
//    [self createScrollView];

    // 调用 TableView
    [self createTableView];
    // 调用菊花
    [self getJUhua];

#pragma mark - 轮播图
    // 第4照片(用于转换)
    UIImageView *bb = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 3.5)];
    bb.image = [UIImage imageNamed:@"113713614.jpg"];
    // ***第4照片放第一位置
    [_scrollView addSubview:bb];
    [bb release];
    
    // 第1照片
    UIImageView *b1 = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 3.5)];
    b1.image = [UIImage imageNamed:@"H_SXSSKLPX06(T`YQYU@9FL.jpg"];
    // 放在第二位置
    [_scrollView addSubview:b1];
    [b1 release];
    
    // 第2照片
    UIImageView *b2 = [[UIImageView alloc]initWithFrame:CGRectMake(2 * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height / 3.5)];
    b2.image = [UIImage imageNamed:@"}3AGM9C]00{%%FUQXF8ZL05.jpg"];
    // 放在第三位置
    [_scrollView addSubview:b2];
    [b2 release];
    
    // 第3照片
    UIImageView *b3 = [[UIImageView alloc] initWithFrame:CGRectMake(3 * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 3.5)];
    b3.image = [UIImage imageNamed:@"2934349b033b5bb5d5732d5634d3d539b700bcf5.jpg"];
    // 放在第四位置
    [_scrollView addSubview:b3];
    [b3 release];
    
    // 第4照片
    UIImageView *b4 = [[UIImageView alloc]initWithFrame:CGRectMake(4 * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 3.5)];
    b4.image = [UIImage imageNamed:@"113713614.jpg"];
    // 放在第五位置
    [_scrollView addSubview:b4];
    [b4 release];
    
    // ***第1照片(用于转换)
    UIImageView *bbb = [[UIImageView alloc]initWithFrame:CGRectMake(5 *[UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 3.5)];
    bbb.image = [UIImage imageNamed:@"H_SXSSKLPX06(T`YQYU@9FL.jpg"];
    // 放在第六位置
    [_scrollView addSubview:bbb];
    [bbb release];

    
    [self addHeader];
    [self addFooter];
}
#warning mark - 下拉刷新
- (void)addHeader
{
    //添加上拉刷新尾部控件
    [self.tv addHeaderWithTarget:self action:@selector(headerRereshing)];
}
- (void)headerRereshing
{
    [self GETDATA];
    
}
#warning mark - 上拉加载
- (void)addFooter
{
    [self.tv addFooterWithTarget:self action:@selector(footerRefreshing)];
    
}
- (void)footerRefreshing
{
    [self getReData];
}
- (void)getReData
{
    YBChinaList *model = [_ArrChina lastObject];
    NSString *url_string = [[NSString stringWithFormat:@"http://api.smzdm.com/v1/youhui/articles?article_date=%@&limit=20",model.article_date] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"********%@",url_string);
        id dic1 = responseObject;
        id dic2 = [dic1 objectForKey:@"data"];
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        arr = [dic2 objectForKey:@"rows"];
        for (NSDictionary *dic in arr) {
            YBChinaList *China = [[YBChinaList alloc]init];
            [China setValuesForKeysWithDictionary:dic];
            [self.arrayImages addObject:China.article_pic];
//            NSLog(@"%@", _arrayImages);
            [_ArrChina addObject:China];
        }
        //        NSLog(@"%ld",_ArrChina.count);
        [_tv reloadData];
        [self.tv footerEndRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
//        NSLog(@"失败==== %@",error);
        
    }];

}
- (void)getJUhua
{
    //小菊花
    self.juhua = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_juhua];
    _juhua.labelText = @"亲 请稍等";
    _juhua.tintColor = [UIColor cyanColor];
    [self.juhua show:YES];
    [_juhua release];

    [self GETDATA];

}
- (void)GETDATA
{
    NSString *url_string = [NSString stringWithFormat:@"http://api.smzdm.com/v1/youhui/articles?limit=20"];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        id dic1 = responseObject;
        id dic2 = [dic1 objectForKey:@"data"];
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        arr = [dic2 objectForKey:@"rows"];
        self.ArrChina = [[NSMutableArray alloc]init];
        self.arrayImages = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            YBChinaList *China = [[YBChinaList alloc]init];
            [China setValuesForKeysWithDictionary:dic];
            [self.arrayImages addObject:China.article_pic];
//            NSLog(@"%@", _arrayImages);
            [_ArrChina addObject:China];
        }
        //        NSLog(@"%ld",_ArrChina.count);
        
        [_tv reloadData];
        [self.tv headerEndRefreshing];
        [self.juhua hide:YES afterDelay:1];
        [self.juhua removeFromSuperview];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
//        NSLog(@"失败==== %@",error);/
        
    }];
}

// -------------创建TableView----------

- (void)createTableView
{
//    self.tv = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    
//    _tv.tableHeaderView = _scrollView;
    CGRect frame = self.view.frame;
    self.tv = [[UITableView alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
    self.tv.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    _tv.delegate = self;
    _tv.dataSource = self;
    _tv.rowHeight = [UIScreen mainScreen].bounds.size.height / 3.5;
    
    //  ----------------分割线
    _tv.separatorStyle = UITableViewCellAccessoryNone;
    [self.view addSubview:_tv];
    [_tv release];
    [_tv registerClass:[ChinaTableViewCell class] forCellReuseIdentifier:@"reuse"];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    ChinaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.chinaList = _ArrChina[indexPath.row];

    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _ArrChina.count;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tv deselectRowAtIndexPath:indexPath animated:YES];
    ChinaDetailViewController *detail = [[ChinaDetailViewController alloc] init];
    detail.article_id = [[_ArrChina objectAtIndex:indexPath.row] article_id];
    
    [self.navigationController pushViewController:detail animated:YES];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UIScreen mainScreen].bounds.size.height / 4;
}
// -----------UIScrollView 滚动视图----------
- (void)createScrollView
{
    // 初始化
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 4)];
    _scrollView.backgroundColor = [UIColor orangeColor];
    // 放置尺寸
    _scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 6, 0);
    // 偏移量
    _scrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
    // 代理
    _scrollView.delegate = self;
    // 整页翻动
    _scrollView.pagingEnabled = YES;
    // 边缘弹动
    _scrollView.bounces = NO;
    // 隐藏滚动条(默认显示)
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    [_scrollView release];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollPages) userInfo:nil repeats:YES];
    

}
- (void)scrollPages
{
    // 利用偏移量使X横轴每次加375换图片
    _scrollView.contentOffset = CGPointMake(_scrollView.contentOffset.x + [UIScreen mainScreen].bounds.size.width , 0);
    // 每次减1  使页码控制器完成切换图片
    _pageControl.currentPage = _scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width - 1;
    
    // 写if判断到第五张图时候  切换到第六张图(也就是第一张假图)  使左右都能滑动
    if (_scrollView.contentOffset.x == [UIScreen mainScreen].bounds.size.width * 5  ) {
        _scrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
        _pageControl.currentPage = 0;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    //偏移量-->页码控制器
    _pageControl.currentPage = _scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width - 1;
    // 向右滑动返回第一张
    if (_scrollView.contentOffset.x == [UIScreen mainScreen].bounds.size.width * 5) {
        _scrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
        _pageControl.currentPage = 0;
        // 向左滑动跳到最后一张
    }else
        if (_scrollView.contentOffset.x == 0) {
            _scrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width * 4, 0);
            _pageControl.currentPage = 4;
            
            
    }

}
// 分页控制器方法
- (void)changePage:(UIPageControl *)chang
{
    _scrollView.contentOffset = CGPointMake(_pageControl.currentPage * [UIScreen mainScreen].bounds.size.width, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
